<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:v-popover="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>商品管理</title>
    <link rel="stylesheet" href="../../../../static/css/element.css" th:href="@{/css/element.css}">
    <link rel="stylesheet" href="../../../../static/css/global.css" th:href="@{/css/global.css}"/>
    <link rel="stylesheet" href="../../../../static/css/main.css" th:href="@{/css/main.css}"/>
    <link rel="stylesheet" href="../../../../static/css/myelement.css" th:href="@{/css/myelement.css}"/>
    <script src="../../../../static/js/vue.js" th:src="@{/js/vue.js}"></script>
    <script src="../../../../static/js/element.js" th:src="@{/js/element.js}"></script>
    <script src="../../../../static/js/jquery.min.js" th:src="@{/js/jquery.min.js}"></script>
    <script src="../../../../static/js/jquery.nestable.js" th:src="@{/js/jquery.nestable.js}"></script>
</head>
<body>
<div id="app">
    <div class="main-content" v-cloak>
        <!-- 页面头部 -->
        <div class="page-header">
            <h1 class="page-title">商品管理</h1>
            <div class="z-pop_question">
                <el-popover
                        ref="popoverq"
                        placement="bottom-start"
                        width="400"
                        trigger="hover">
                    <p class="z-jiao">
                        1. 查看商品列表
                        2. 进行添加编辑删除
                        <a href="javascript:;">查看更多帮助</a>
                    </p>
                </el-popover>
                <i v-popover:popoverq class="el-icon-question"></i>
            </div>
            <div class="breadcrumb"><a href="javascript: top.location.reload();">首页</a><span>/</span>商品<span>/</span>商品管理</div>
        </div><!-- 页面头部 end -->

        <div class="list-content">
            <!--条件筛选-->
            <div class="content_item_filter clearfix" :class="[hasfilterMore?'content_filter_more':'']">
                <div class="input-select fl btn-radid_none">
                    <div class="filter-form filter-form-one clearfix">
                        <el-select placeholder="商品名称" v-model="conditions.categoryUuid.value" clearable class="z_select-holder" @visible-change="validateVendor">
                            <el-option v-for="cate in productCateList"
                                       :key="cate.uuid"
                                       :label="cate.name"
                                       :value="cate.uuid">
                            </el-option>
                        </el-select>
                    </div>
                    <div class='filter-form filter-form-more clearfix' v-show="filterMore"></div>
                    <div class="filter-operate">
                        <div class="filter-more" @click="toggleMoreSearch" v-show="hasfilterMore"><i :class="[filterMore?'el-icon-arrow-up':'el-icon-arrow-down']"></i></div>
                        <el-tooltip class="item" content="查询" placement="bottom-start">
                            <el-button icon="el-icon-search" @click="handleQuery"></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" content="清空" placement="bottom-start">
                            <el-button icon="el-extend-clean" @click="handleClear"></el-button>
                        </el-tooltip>
                    </div>
                </div>
                <div class="filter-btn fr">
                    <el-button type="primary" @click="toAddProduct">新增菜品</el-button>
                </div>
            </div>

            <!--主体表格-->
            <div class="z-tables">
                <el-table ref="multipleTable" :data="productList" style="width: 100%;">
                	<el-table-column label="商品图片" min-width="120">
                		<template slot-scope="scope">
                    		<div class="product-table">
	                    		<span class='img'>
		                    		<div v-if="!!scope.row.smallimg"><img :src="scope.row.smallimg" style="width: 50px;height: 50px"></div>
		                    		<div v-else><img src="../../../../img/noimage.jpg" style="width: 80px;height: 80px"></div>
	                    		</span>
							</div>
                        </template>
                	</el-table-column>
                    <el-table-column prop="name" label="菜品名称" min-width="120"></el-table-column>
                    <el-table-column prop="name" label="菜品标签" min-width="120"></el-table-column>
                    <el-table-column prop="price" label="价格" min-width="120"></el-table-column>
                    <el-table-column label="主图" min-width="120">
                        <template slot-scope="scope">
                            <img :src="scope.row.smallimg" width="50" height="50"/>
                        </template>
                    </el-table-column>
                    <el-table-column prop="vendorName" label="所属商户" min-width="120"></el-table-column>
                    <el-table-column prop="tagName" label="标签" min-width="120"></el-table-column>
                    <el-table-column label="状态" min-width="120">
                        <template slot-scope="scope">
                            <template v-if="scope.row.state == '1'">
                                已上架
                            </template>
                            <template v-else>
                                未上架
                            </template>
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="创建时间" min-width="180" class-name="table-operate-td">
                        <template slot-scope="scope">
                            <div class="table-info">{{ scope.row.createTime }}</div>
                            <div class="table-operate">
                                <el-button type="primary" @click="handleEdit(scope.$position, scope.row)">编辑</el-button>
                                <span class="table-spea-line">|</span>
                                <el-button type="primary" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <!--分页-->
            <div class="grid-content">
                <div class="bg-purple-light">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="currentPage"
                            :page-sizes="pageSizeList"
                            :page-size="pageSize"
                            layout="total, sizes, prev, pager, next"
                            :total="totalNum">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script th:inline="javascript">
    app = new Vue({
        el: "#app",
        data: {
            loading: true,
            openText: "",
            dialogFormVisible: false,
            valueId: '',
            valueState: '',
            tableAllCheck: false,  // table 下方批量操作全选
            hasfilterMore: false,  // 列表是否拥有更多筛选条件
            filterMore: false,  // 列表更多筛选条件是否展示
            conditions: {
                "vendorUuid": {
                    "operation": "EQ",
                    "value": undefined
                },
                "categoryUuid":{
                    "operation": "EQ",
                    "value": undefined
                }
            },
            dialogVisible: false,
            pageSizeList: [5, 10, 20, 50],
            totalNum: 0,
            currentPage: 1, //当前页码数
            pageSize: 10,
            rules: {
                password: [
                    {required: true, message: "请输入登录密码", trigger: 'blur'},
                    {min: 6, max: 20, message: '密码长度6到20个字符', trigger: 'blur'}
                ]
            },
            productList:[],
            productCateList:[],
            vendorList:[[${vendorList}]],
        },
        created: function () {
            queryList();
        },
        methods: {
            // 选择商品分类条件时，先要判断是否选择了商户
            validateVendor:function(show){
                if(show) {
                    if (this.conditions.vendorUuid.value == "" || this.conditions.vendorUuid.value == undefined) {
                        this.$message({
                            duration: 1500,
                            message: '请先选择所属商户!',
                            type: 'waring',
                            customClass: 'message-center'
                        });
                    }
                }
            },
            // 跳转到商品添加页面
            toAddProduct: function () {
                window.location.href = "/vendor/cater/product/toadd";
            },
            // 跳转到商品编辑页面
            handleEdit(position, row) {
                window.location.href = "/vendor/cater/product/toedit" + "/" + row.uuid;
            },
            dislogHide: function () {
                this.dialogFormVisible = false;
            },
            // 筛选条件选择商户后，加载出商户下的桌台类型
            vendorSelect: function () {
                var self = this;
                // 获取当前选中的商户uuid
                let vendorUuid = self.conditions.vendorUuid.value;
                $.ajax({
                    url: "/vendor/cater/productcate/listByVendorUuid?vendorUuid="+vendorUuid,
                    type: "POST",
                    success: function(data) {
                        if (data && data.returnCode == "1") {
                            self.conditions.categoryUuid.value = undefined;
                            self.productCateList = data.returnMessage;
                        }
                    }
                });
            },
            identityFormatter: function (row, column, cellValue, position) {
                for (let identity of this.identityList) {
                    if (identity.value === cellValue) {
                        return identity.name;
                    }
                }
            },
            handleSizeChange: function (val) {
                this.pageSize = val;
                queryList();
            },
            handleCurrentChange: function (val) {
                this.currentPage = val;
                queryList();
            },
            handleQuery() {
                this.currentPage = 1;
                queryList();
            },
            handleClear() {
                this.conditions.vendorUuid.value = undefined;
                this.conditions.categoryUuid.value = undefined;
            },
            // 删除
            handleDelete(index, row) {
                let self = this;
                self.$confirm('确认要删除此商品信息?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(() => {
                    $.ajax({
                        url: "/vendor/cater/product/delete",
                        type: "POST",
                        data: JSON.stringify([row.uuid]),
                        contentType: "application/json;charset=UTF-8",
                        dataType: "json",
                        success: function (data) {
                            if (data && data.returnCode === 1) {
                                self.$message({
                                    message: '删除商品成功',
                                    type: 'success',
                                    customClass: 'message-center',
                                    duration: 1500,
                                });
                                self.handleQuery();
                            } else {
                                self.$message({
                                    message: '删除商品失败',
                                    type: 'warning',
                                    customClass: 'message-center',
                                    duration: 1500,
                                });
                            }
                        }
                    });
                });
            },
            handleCommand(command) {
                this.$message('click on item ' + command);
            },
            // 列表批量操作2-批量导出
            handleCommand2(command) {
                this.$message('click on item ' + command);
            },
            tableAllCheckFn(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row, this.tableAllCheck)
                    })
                }
            },
            toggleMoreSearch() {
                this.filterMore = !this.filterMore;
            },
        }
    });

    function queryList() {
        app.loading = true;
        let queryMessage = {
            "queryPage": app.currentPage,
            "pageShow": app.pageSize,
            "queryParams": app.conditions,
            "sortMap": {
                "uuid": "desc"
            }
        };
        $.ajax({
            url: "/vendor/cater/product/list",
            type: "POST",
            data: JSON.stringify(queryMessage),
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: function (data) {
                if (data && data.returnCode === 1) {
                    app.productList = data.returnMessage;
                    app.totalNum = data.pager.totalNum;
                }
                app.loading = false;
            }
        });
    }
</script>
</html>